package job2;

import java.util.concurrent.Semaphore;

/**
 * 同步组件:Semaphore
 */
public class Main11 {

    private static volatile int result = -1;

    private static final Semaphore semaphore = new Semaphore(1);

    public static void main(String[] args) throws Exception {
        long start = System.currentTimeMillis();
        semaphore.acquire();

        // 异步执行 下面方法
        Thread thread = new Thread(() -> {
            result = sum();
            try {
                semaphore.release();
            } catch (Exception e) {
            }
        });
        thread.start();

        semaphore.acquire();
        // 确保  拿到result 并输出
        System.out.println("异步计算结果为：" + result);
        System.out.println("使用时间：" + (System.currentTimeMillis() - start) + " ms");

        semaphore.release();
    }

    private static int sum() {
        return fibo(36);
    }

    private static int fibo(int a) {
        if (a < 2)
            return 1;
        return fibo(a - 1) + fibo(a - 2);
    }

}
